package com.paas.form.core.element.base;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gencode.util.CodeBuildNew;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.Element;
import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.control.FormItemFactory;
import com.paas.form.core.element.field.Config;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.field.FormDataModel;
import com.paas.form.core.element.jsContent.ScriptContent;
import com.paas.form.core.enums.ElementUIEnum;
import com.paas.form.core.model.*;
import com.paas.form.core.model.js.*;

import java.io.*;
import java.util.*;

public class Form implements Element {


    /**
     * @param formDataModel
     * @return
     */
    public String getFormHtml(FormDataModel formDataModel, Map<String, Object> configMap) {
        //js各部分内容
        ScriptContent scriptContent = new ScriptContent();
        //
        StringBuilder str = new StringBuilder();

        Map<String, Object> map = new HashMap<>();
        map.put("formObject",formDataModel.getFormModel());

        // ---------------------vue---------------------------
        Template template = new Template(0);
        template.getStartTagElHtml(str);
        // el-form标签
        ElForm elForm = new ElForm(formDataModel, 2);
        elForm.getStartTagElHtml(str);
        // 获取转换后的表单配置字段集合
        JSONArray fiedldsJsarray= JSONArray.parseArray(formDataModel.getFields());
        List<FieLdsModel> formFields = new ArrayList<>();
        for (int i = 0; i <fiedldsJsarray.size() ; i++) {
            JSONObject fieldJson = fiedldsJsarray.getJSONObject(i);
            Map<String,Object> fieldNewMap = new HashMap();
            Set<String> keys = fieldJson.keySet();
            for (String key:keys) {
                String newkey = key.replaceAll("-","");
                fieldNewMap.put(newkey,fieldJson.get(key));
            }
            FieLdsModel fieldsModel = JSONObject.parseObject(JSONObject.toJSONString(fieldNewMap), FieLdsModel.class);
            if(fiedldsJsarray.getJSONObject(i).get("vModel") != null){
                fieldsModel.setVModel((String) fiedldsJsarray.getJSONObject(i).get("vModel"));
            }
            formFields.add(fieldsModel);
        }
        List<List<FieLdsModel>> configFieldList = assembleConfigFieldList(formFields);
        for (List<FieLdsModel> clist : configFieldList) {
            // el-row标签
            ElRow elRow = new ElRow(3);
            elRow.getStartTagElHtml(str);
            for (FieLdsModel fieLdsModel : clist) {
                // el-col标签
                ElCol elCol = new ElCol(fieLdsModel, 4);
                elCol.getStartTagElHtml(str);
                // el-form-item标签
                ElFormItem elFormItem = new ElFormItem(fieLdsModel, 5);
                elFormItem.getStartTagElHtml(str);
                //
//                getBaseFormElements(formDataModel.getFormModel(), fieLdsModel, str, 6);
                map.put("field",fieLdsModel);
                FormItem formItem = FormItemFactory.create(fieLdsModel,"form");
                String formItemHtml = formItem.makeVue();
                if(StringUtil.isNotEmpty(formItemHtml)){
                    str.append(CodeBuildNew.getCodeString(map, formItemHtml));
                }
                ScriptContent itemScript = formItem.makeJs();
                if(BeanUtil.isNotEmpty(itemScript)){
                    //把个item模块的js/data/methods 拼接
                    if(StringUtil.isNotEmpty(itemScript.getComponents())){
                        String newComStr = CodeBuildNew.getCodeString(map,itemScript.getComponents());
                        itemScript.setComponents(newComStr);
                    }
                    if(StringUtil.isNotEmpty(itemScript.getData())){
                        String newDataStr = CodeBuildNew.getCodeString(map,itemScript.getData());
                        itemScript.setData(newDataStr);
                    }
                    if(StringUtil.isNotEmpty(itemScript.getMethods())){
                        String newMethodsStr = CodeBuildNew.getCodeString(map,itemScript.getMethods());
                        itemScript.setMethods(newMethodsStr);
                    }
                    if(StringUtil.isNotEmpty(itemScript.getProps())){
                        String newPropsStr = CodeBuildNew.getCodeString(map,itemScript.getProps());
                        itemScript.setProps(newPropsStr);
                    }
                }
                scriptContent = Utilmethod.scriptContentAdd(scriptContent,itemScript);
                //
                elFormItem.getEndTagElHtml(str);
                elCol.getEndTagElHtml(str);
            }
            elRow.getEndTagElHtml(str);
        }
        elForm.getEndTagElHtml(str);
        //保存与取消按钮，保存方法

        DialogButton dialogButton = new DialogButton(2);
        str.append(dialogButton.getDialogButtonHtml());
        scriptContent = Utilmethod.scriptContentAdd(scriptContent,dialogButton.makeJs());

        template.getEndTagElHtml(str);


        // --------------------Script---------------------------------
        Script script = new Script(0);
        script.getStartTagElHtml(str);
        //
        VueImport vueImport = new VueImport(0);
        vueImport.getElHtml(str);
        if(StringUtil.isNotEmpty(scriptContent.getImports())){
            str.append(scriptContent.getImports());
        }
        //
        Export export = new Export(0);
        export.getStartTagElHtml(str);
        //props
        VueProps vueProps = new VueProps(1);
        vueProps.getStartTagElHtml(str);
        if(StringUtil.isNotEmpty(scriptContent.getProps())){
            str.append(scriptContent.getProps());
        }
        vueProps.getEndTagElHtml(str);
        //components
        VueComponents vueComponents = new VueComponents(1);
        vueComponents.getStartTagElHtml(str);
        if(StringUtil.isNotEmpty(scriptContent.getComponents())){
            str.append(scriptContent.getComponents());
        }
        vueComponents.getEndTagElHtml(str);
        //data
        VueData vueData = new VueData(1);
        vueData.getStartTagElHtml(str);
        vueData.s(str, formDataModel);
        if(StringUtil.isNotEmpty(scriptContent.getData())){
            str.append(scriptContent.getData());
        }
        vueData.getEndTagElHtml(str);
        //
        VueMethods vueMethods = new VueMethods(1);
        vueMethods.getStartTagElHtml(str);
        if(StringUtil.isNotEmpty(scriptContent.getMethods())){
            str.append(scriptContent.getMethods());
        }
        vueMethods.getEndTagElHtml(str);
        //
        export.getEndTagElHtml(str);
        script.getEndTagElHtml(str);

        String formVue = str.toString();
        formVue =  CodeBuildNew.getCodeString(configMap,formVue);
        return formVue;
    }

    /**
     * 重新组装表单配置字段集合
     * @param fields 未组装后表单字段集合
     * @return List<List < Config>>
     */
    public List<List<FieLdsModel>> assembleConfigFieldList(List<FieLdsModel> fields) {
        // 表单配置字段集合-集合
        List<List<FieLdsModel>> configFieldList = new ArrayList<List<FieLdsModel>>();
        // 表单配置字段集合
        List<FieLdsModel> list = new ArrayList<>();
        // 标签el-col中span属性
        int _col_span = 0;
        // 循环根据span值筛选表单配置字段-筛选规则
        for (int i = 0; i < fields.size(); i++) {
            //
            FieLdsModel fieLdsModel = fields.get(i);
            // 配置字段信息
            Config config = fieLdsModel.getConfig();
            // span属性赋值
            _col_span = _col_span + config.getSpan();
            // 筛选
            if (_col_span == 24) {
                list.add(fieLdsModel);
                configFieldList.add(list);
                _col_span = 0;
                list = new ArrayList<>();
            }
            //
            else if (_col_span >= 24) {
                configFieldList.add(list);
                list = new ArrayList<>();
                list.add(fieLdsModel);
            }
            //
            else {
                list.add(fieLdsModel);
            }
            //
            if (i == fields.size() - 1) {
                configFieldList.add(list);
            }
        }
        return configFieldList;
    }

    /**
     * 获取基础表单元素表单
     * @param fieLdsModel 字段配置信息
     * @param str
     * @param indentNum   字符缩进
     */
    public static void getBaseFormElements(String prefixName, FieLdsModel fieLdsModel, StringBuilder str, int indentNum) {
        switch (fieLdsModel.getConfig().getTag()) {
            case "el_button":
                ElButton elButton = new ElButton(fieLdsModel, indentNum);
                elButton.getElHtml(str);
                break;
            case "el_input":
                ElInput elInput = new ElInput(prefixName, fieLdsModel, indentNum);
                elInput.getElHtml(str);
                break;
            case "el_select":
                ElSelect elSelect = new ElSelect(prefixName, fieLdsModel, indentNum);
                elSelect.getStartTagElHtml(str);
                elSelect.getEndTagElHtml(str);
                break;
            default:
                DivComponent divComponent = new DivComponent(prefixName, fieLdsModel, indentNum);
                divComponent.getStartTagElHtml(str);
                divComponent.getEndTagElHtml(str);
                break;
        }
    }


//    public static void fullAttribute(FieLdsModel fieLdsModel, StringBuilder str, int indentNum) {
//        switch (ElementUIEnum.matchOpCode(fieLdsModel.get__config__().getTag())) {
//            case el_button:
//                ElButton elButton = new ElButton(fieLdsModel, indentNum);
//                elButton.getElHtml(str);
//                break;
//            case el_input:
//                ElInput elInput = new ElInput(fieLdsModel, indentNum);
//                elInput.getElHtml(str);
//                break;
//            case el_select:
//                ElSelect elSelect = new ElSelect(fieLdsModel, indentNum);
//                elSelect.getStartTagElHtml(str);
//                elSelect.getEndTagElHtml(str);
//                break;
//            default:
//        }
//    }


    @Override
    public String makeVue() {
        return null;
    }

    @Override
    public ScriptContent makeJs() {
        return null;
    }
}
